﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;


namespace SecuritySystem
{
    public partial class InventoryListingForm : Form
    {
        TIEntities dbContext;
        
        public InventoryListingForm()
        {
            InitializeComponent();
            dbContext = new TIEntities();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonAddInventory_Click(object sender, EventArgs e)
        {
            AddEditInventory form = new AddEditInventory();


            form.MdiParent = this.MdiParent;

            form.Show();
            form.WindowState = FormWindowState.Minimized;
            form.WindowState = FormWindowState.Maximized;
        }

        private void RefreshGrid()
        {
            try
            {
                //var listOfInventories = (from sup in dbContext.Inventories select sup).ToList<Inventory>();
                var listOfInventories = (from sup in dbContext.Inventories select new { sup.ID, sup.SupplierID, sup.ProductTypeID, sup.ProductSubTypeID, sup.SizeID, sup.ItemCode, sup.ItemName, sup.Description, sup.Price, sup.Quantity, sup.Supplier.ContactPersonName, productTypeName = sup.ProductType.Name, productSubTypeName = sup.ProductSubType.Name, productSizeName = sup.ProductSize.Name, sup.PurchaseDate }).ToList();
                dataGridViewInventory.DataSource = listOfInventories;

                dataGridViewInventory.Columns[0].Visible = false;
                dataGridViewInventory.Columns[1].Visible = false;
                dataGridViewInventory.Columns[2].Visible = false;
                dataGridViewInventory.Columns[3].Visible = false;
                dataGridViewInventory.Columns[4].Visible = false;
                
                dataGridViewInventory.Columns["ItemCode"].HeaderCell.Value = "Item Code";
                dataGridViewInventory.Columns["ItemName"].HeaderCell.Value = "Item Name";
                dataGridViewInventory.Columns["Price"].HeaderCell.Value = "Purchase Price";
                dataGridViewInventory.Columns["Quantity"].HeaderCell.Value = "Quantity Purchased";                
                dataGridViewInventory.Columns["Description"].HeaderCell.Value = "Description";
                dataGridViewInventory.Columns["ContactPersonName"].HeaderCell.Value = "Supplier Name";
                dataGridViewInventory.Columns["productTypeName"].HeaderCell.Value = "Product Type";
                dataGridViewInventory.Columns["productSubTypeName"].HeaderCell.Value = "Product Sub Type";
                dataGridViewInventory.Columns["productSizeName"].HeaderCell.Value = "Size";
                dataGridViewInventory.Columns["PurchaseDate"].HeaderCell.Value = "Purchase Date";

            }
            catch (Exception e1)
            {


            }
        }

        private void InventoryListingForm_Load(object sender, EventArgs e)
        {
            RefreshGrid();
            
        }

        private void buttonEditSelected_Click(object sender, EventArgs e)
        {
            AddEditInventory form = new AddEditInventory();

            form.selectedRecord = dataGridViewInventory.SelectedRows[0];
            form.MdiParent = this.MdiParent;

            form.Show();
            form.WindowState = FormWindowState.Minimized;
            form.WindowState = FormWindowState.Maximized;
        }

        private void buttonDeleteSelected_Click(object sender, EventArgs e)
        {
            int id = int.Parse(dataGridViewInventory.SelectedRows[0].Cells["ID"].Value.ToString());
            Inventory sup = (from s in dbContext.Inventories where s.ID == id select s).FirstOrDefault();
            dbContext.DeleteObject(sup);
            dbContext.SaveChanges();
            RefreshGrid();
        }

        private void button1_Click(object sender, EventArgs e)
        {
         

        }

        private void buttonDeleteSelected_ControlRemoved(object sender, ControlEventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            RefreshGrid();
        }

      
    }
}
